﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CloudFilesUpload.Helper;
using com.mosso.cloudfiles.domain;
using com.mosso.cloudfiles;
using System.IO;

namespace CloudFilesUpload
{
    /// <summary/>
    public class Program
    {
        /// <summary/>
        static int Main(string[] arguments)
        {
            // check the input count
            if (arguments.Length != Constant.ARGUMENT_COUNT)
            {
                // write the output
                Console.Write(ResourceHelper.Fetch(Constant.TYPE_FULL_NAME_USAGE));

                // an error occured
                return Constant.EXIT_CODE_FAILURE;
            }

            try
            {
                string userName = arguments[0];
                string key = arguments[1];
                string container = arguments[2];
                string filePath = arguments[3];

                // create the credentials
                UserCredentials userCredentials = new UserCredentials(
                    userName,
                    key);

                Connection connection = new Connection(userCredentials);

                using (Stream stream = new FileStream(filePath, FileMode.Open))
                {
                    connection.PutStorageItem(container, stream, Path.GetFileName(filePath));
                }

                // success
                return Constant.EXIT_CODE_SUCCESS;
            }
            catch (Exception exception)
            {
                // write the output
                Console.Write(exception.Message);

                // an error occured
                return Constant.EXIT_CODE_FAILURE;
            }
        }
    }
}
